package com.zua.controller;
import com.github.pagehelper.PageInfo;
import com.zua.entity.Order;
import com.zua.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@CrossOrigin(origins = "*",allowCredentials = "true")
@RestController
@RequestMapping("Order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    /**
     * 获取所有订单
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("orders")
    public Map showAll(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "5")Integer pageSize){
        HashMap<String, Object> map = new HashMap<>();
        List<Order> list = orderService.selectAll(pageNum, pageSize);
        PageInfo<Order> pageInfo = new PageInfo<>(list,10);
        long total = pageInfo.getTotal();
        int[] pages = pageInfo.getNavigatepageNums();
        map.put("list",list);
        map.put("pages",pages);
        map.put("total",total);
        return map;
    }
    /**
     * 完成订单
     * @param id
     * @return
     */
    @GetMapping("/finishOrder/{id}")
    public Map finishOrder(@PathVariable("id") Integer id){
        HashMap<String, Object> map = new HashMap<>();
        try {
            orderService.finishOrder(id);
            map.put("state","success");
        } catch (Exception e) {
            map.put("state","failed");
            e.printStackTrace();
        }
        return map;
    }
    /**
     * 删除订单
     * @param id
     * @return
     */
    @DeleteMapping("/order/{id}")
    public Map removeOrder(@PathVariable("id") Integer id){
        HashMap<String, Object> map = new HashMap<>();
        try {
            orderService.deleteById(id);
            map.put("state","success");
        } catch (Exception e) {
            map.put("state","failed");
            e.printStackTrace();
        }
        return map;
    }
}
